package com.demo.servicelmpl;

import com.demo.bean.ConflictObjects;
import com.demo.bean.DailyActivity;
import com.demo.bean.FourNums;
import com.demo.mapper.DailyActivityMapper;
import com.demo.service.DailyActivityService;
import com.demo.service.RedisService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.LinkedHashMap;

/**
 * @author 李天航
 * 日常活动的实现类
 */
@Service
public class DailyActivityServiceImpl implements DailyActivityService {

    @Resource
    DailyActivityMapper dailyActivityMapper;
    @Resource
    RedisService redisService;

    public static int MAX_FIXED_EVENT_NUM =20;

    private ConflictObjects judgeConflict(DailyActivity dailyActivity)
    {
        DailyActivity dailyActivity1=dailyActivityMapper.firstLargerBeginTime(dailyActivity);
        if(dailyActivity1!=null&&dailyActivity.getEndTime()!=null&&dailyActivity1.getBeginTime()<dailyActivity.getEndTime()) {
            return new ConflictObjects(false,dailyActivity1,dailyActivity);
        }
        DailyActivity dailyActivity2=dailyActivityMapper.firstLargerEndTime(dailyActivity);
        if(dailyActivity2!=null&&dailyActivity.getBeginTime()!=null&&dailyActivity2.getBeginTime()<dailyActivity.getBeginTime()) {
            return new ConflictObjects(false,dailyActivity2,dailyActivity);
        }
        System.out.println(dailyActivity);
        System.out.println(dailyActivity1);
        System.out.println(dailyActivity2);
        return new ConflictObjects(true,null,null);
    }

    @Override
    public LinkedHashMap<String, Object> addDailyActivity(DailyActivity dailyActivity) {
        LinkedHashMap<String, Object> resultMap=new LinkedHashMap<>();
        try {
            if(dailyActivity.getBeginTime()>dailyActivity.getEndTime())
            {
                resultMap.put("status",500);
                resultMap.put("msg","起始时间不能大于终止时间");
                return resultMap;
            }
            ConflictObjects conflictDailyActivity=judgeConflict(dailyActivity);
            if(!conflictDailyActivity.isNotConflict)
            {
                resultMap.put("status",500);
                resultMap.put("msg","活动与其他活动冲突");
                resultMap.put("conflictDailyActivity",conflictDailyActivity.newObject);
                resultMap.put("addDailyActivity",conflictDailyActivity.rawObject);
            }
            else
            {
                if(dailyActivityMapper.getDailyActivityCount(dailyActivity.getUserAccount())<MAX_FIXED_EVENT_NUM) {
                    dailyActivityMapper.addDailyActivity(dailyActivity);
                    resultMap.put("status", 200);
                    resultMap.put("msg", "添加成功");
                }
                else
                {
                    resultMap.put("status", 500);
                    resultMap.put("msg", "数量达到上限");
                }

            }
        }catch (Exception e)
        {
            e.printStackTrace();
            resultMap.put("status",500);
            resultMap.put("msg",e.getMessage());
        }
        return resultMap;
    }

    @Override
    public LinkedHashMap<String, Object> deleteDailyActivity(int dailyId, String userAccount) {
        LinkedHashMap<String, Object> resultMap=new LinkedHashMap<>();
        try {
            DailyActivity dailyActivity= dailyActivityMapper.getDailyActivityById(dailyId);
            if(!dailyActivity.getUserAccount().equals(userAccount))
            {
                resultMap.put("status",500);
                resultMap.put("msg","无修改权限");
            }
            else {
                dailyActivityMapper.deleteDailyActivity(dailyId);
                resultMap.put("status", 200);
                resultMap.put("msg", "删除成功");
            }
        }catch (Exception e)
        {
            e.printStackTrace();
            resultMap.put("status",500);
            resultMap.put("msg",e.getMessage());
        }
        return resultMap;
    }

    @Override
    public LinkedHashMap<String, Object> getDailyActivitiesByUserAccount(String userAccount) {
        LinkedHashMap<String, Object> resultMap=new LinkedHashMap<>();
        try {
            resultMap.put("dailyActivities",dailyActivityMapper.getDailyActivitiesByUserAccount(userAccount));
            resultMap.put("status",200);
            resultMap.put("msg","查询成功");
        }catch (Exception e)
        {
            e.printStackTrace();
            resultMap.put("status",500);
            resultMap.put("msg",e.getMessage());
        }
        return resultMap;
    }

    @Override
    public LinkedHashMap<String, Object> updateDailyActivity(DailyActivity dailyActivity) {
        LinkedHashMap<String, Object> resultMap=new LinkedHashMap<>();
        try {
            if(dailyActivity.getBeginTime()==null&&dailyActivity.getEndTime()==null)
            {
                dailyActivityMapper.updateDailyActivity(dailyActivity);
                resultMap.put("status", 200);
                resultMap.put("msg", "修改成功");
                return resultMap;
            }
            DailyActivity rawActivity=dailyActivityMapper.getDailyActivityById(dailyActivity.getDailyId());
            if(dailyActivity.getUserAccount().equals(rawActivity.getUserAccount())) {
                if(dailyActivity.getBeginTime()!=null) {
                    rawActivity.setBeginTime(dailyActivity.getBeginTime());
                }
                if(dailyActivity.getEndTime()!=null) {
                    rawActivity.setEndTime(dailyActivity.getEndTime());
                }
                if(rawActivity.getBeginTime()> rawActivity.getEndTime())
                {
                    resultMap.put("status",500);
                    resultMap.put("msg","起始时间不能大于终止时间");
                    return resultMap;
                }
                ConflictObjects conflictDailyActivity=judgeConflict(rawActivity);
                if(conflictDailyActivity.isNotConflict) {
                    dailyActivityMapper.updateDailyActivity(dailyActivity);
                    resultMap.put("status", 200);
                    resultMap.put("msg", "修改成功");
                }
                else
                {
                    resultMap.put("status", 500);
                    resultMap.put("msg", "与其他活动冲突");
                    resultMap.put("updateDailyActivity",conflictDailyActivity.rawObject);
                    resultMap.put("conflictDailyActivity",conflictDailyActivity.newObject);
                }
            }
            else
            {
                resultMap.put("status", 500);
                resultMap.put("msg", "无修改权限");
            }
        }catch (Exception e)
        {
            e.printStackTrace();
            resultMap.put("status",500);
            resultMap.put("msg",e.getMessage());
        }
        return resultMap;
    }

    @Override
    public LinkedHashMap<String, Object> getDailyActivityById(int dailyActivityId) {
        LinkedHashMap<String, Object> resultMap=new LinkedHashMap<>();
        try {
            resultMap.put("dailyActivities",dailyActivityMapper.getDailyActivityById(dailyActivityId));
            resultMap.put("status",200);
            resultMap.put("msg","查看成功");
        }catch (Exception e)
        {
            e.printStackTrace();
            resultMap.put("status",500);
            resultMap.put("msg",e.getMessage());
        }
        return resultMap;
    }
}
